import 'package:enterprise_v6_app/app/components/dialog/exit_tips.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/utils/module_permissions_util.dart';
import 'package:enterprise_v6_app/app/data/entities/application/securtity_base_drawer.dart';
import 'package:enterprise_v6_app/app/modules/application/intelligent_inspection_page/home_inspection_page/home_inspection_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/intelligent_inspection_page/home_inspection_page/home_inspection_view.dart';
import 'package:enterprise_v6_app/app/modules/application/intelligent_inspection_page/inspec_all_view_page/inspec_all_view_view.dart';
import 'package:enterprise_v6_app/app/modules/application/intelligent_inspection_page/inspection_list_page/inspection_list_view.dart';
import 'package:enterprise_v6_app/app/modules/application/intelligent_inspection_page/manage_inspection_page/manage_inspection_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/intelligent_inspection_page/manage_inspection_page/manage_inspection_view.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

class IntelligentInspectionController extends GetxController {
  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 展开收起控制key
  final scaffoldKey = GlobalKey<ScaffoldState>();

  /// 选中的页面
  int selectIndex = 0;

  // 标题列表
  final titleList = [
    SecurityBaseEntity(
      title: '总览',
      icon: AppImages.icon_inspection_drawer_4,
      expandTitle: '总览',
      currentPageTitle: 0,
      pages: [InspecAllViewPage()],
      path: '/home',
    ),
    SecurityBaseEntity(
        title: '本部巡检',
        icon: AppImages.icon_inspection_drawer_1,
        expandTitle: '本部巡检计划',
        currentPageTitle: 0,
        pages: [HomeInspectionPage()],
        path: '/homeInspection'),
    SecurityBaseEntity(
        title: '管理巡检',
        icon: AppImages.icon_inspection_drawer_2,
        expandTitle: '管理巡检计划',
        currentPageTitle: 0,
        pages: [ManageInspectionPage()],
        path: '/manageInspection'),
    SecurityBaseEntity(
      title: '巡检计划台账',
      icon: AppImages.icon_inspection_drawer_3,
      expandTitle: '巡检计划台账',
      currentPageTitle: 0,
      pages: [InspectionListPage()],
      path: '/inspectionList',
    ),
  ];

  @override
  void onInit() {
    super.onInit();
    initModule();
  }

  /// 根据后台权限初始化菜单
  void initModule() {
    ModulePermissionsUtil.removeRoleMenu(titleList: titleList, routes: AppRoutes.intelligentInspection);
    update();
  }

  /// 点击左侧菜单
  /// [index] 索引
  void selectLeftMenu(int index) async {
    /// 判断点击其他功能或返回按钮，即领取任务>0，
    /// 弹窗对话：“将清空之前操作，确认继续吗？”
    /// 如果用户继续则清空其之前的操作，并跳转至对应功能，若不是，则保留之前操作，且不跳转页面
    if (selectIndex == 1 && index != 1) {
      final HomeInspectionController controller1 = Get.find();
      if (controller1.selectTaskIdList.isNotEmpty) {
        // 弹窗提示
        Get.dialog<bool>(ExitTips(
          content: '将清空之前操作，确认继续吗？',
          func: (v) {
            if (v) {
              controller1.clearSelected();
              selectIndex = index;
              update();
            } else {
              return false;
            }
          },
        ));
      } else {
        selectIndex = index;
        update();
      }
    } else if (selectIndex == 2 && index != 2) {
      // 管理巡检逻辑
      final ManageInspectionController controller2 = Get.find();
      if (controller2.selectTaskIdList.isNotEmpty) {
        // 弹窗提示
        Get.dialog<bool>(ExitTips(
          content: '将清空之前操作，确认继续吗？',
          func: (v) {
            if (v) {
              controller2.clearSelected();
              selectIndex = index;
              update();
            } else {
              return false;
            }
          },
        ));
      } else {
        selectIndex = index;
        update();
      }
    } else {
      selectIndex = index;
      update();
    }
  }

  /// 点击展开菜单
  /// [index] 索引
  void selectExpandMenu(int index) {
    selectIndex = index;
    Get.back();
    update();
  }
}
